<template>
  <div class="fullscreen bg-white text-blue text-center q-pa-md flex flex-center">
    <div class="column">
      <div class="col">
        <q-spinner-ios
          v-show="load_show"
          color="primary"
          size="3em"
        />
        <q-spinner-radio
          v-show="tip_show"
          color="red"
          size="3em"
        />
      </div>
      <div class="col">
        {{ tip_text }}
      </div>
      <div class="col">
        <img src="" />
      </div>
      <div class="col q-pa-xl">
        <q-btn color="black" class="full-width" label="尝试重新授权" @click="onClick" />
      </div>
    </div>
  </div>
</template>

<script>
import { wxOauthCode } from 'boot/utils'
export default {
  name: 'PageLogin',
  components: {
  },
  data () {
    return {
      load_show: true,
      tip_show: false,
      tip_text: '正在授权中...'
    }
  },
  computed: {
  },
  mounted () {},
  watch: {},
  created () {
    this.onCheckQuery()
  },
  methods: {
    // 验证url正确性
    onCheckQuery () {
      const objQuery = this.$route.query
      if (this.$_.hasIn(objQuery, 'agentId') && !objQuery.agentId) {
        this.$router.push({ name: 'error' })
      } else {
        if (this.$_.hasIn(objQuery, 'code') && objQuery.code) {
          this.getWxOauthUser(objQuery)
        } else {
          wxOauthCode(objQuery)
        }
      }
    },
    getWxOauthUser (objQuery) {
      const item = {
        AgentId: objQuery.agentId,
        Code: objQuery.code
      }
      this.$store.dispatch('oauth/getWxUser', item)
        .then(res => {
          if (res.code == 403) {
            this.tip_text = res.message
            this.load_show = false
            this.tip_show = true
          }
          if (res.code == 200 && res.data) {
            this.$q.localStorage.set('qwscrm_saas_header_local', res.data.header_data)
            this.$q.localStorage.set('qwscrm_saas_user_info', res.data.user_data)
            this.$q.localStorage.set('qwscrm_saas_agent_info', res.data.agent_data)

            this.$router.push({ name: 'home' })
          }
          console.log('res1', res)
        })
        .catch(error => {
          console.log('error', error)
        })
    },
    onClick () {
      window.location.href = this.$q.localStorage.getItem('qwscrm_saas_home_url')
    }
  }
}
</script>
